package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.io.IOException;


public class LoginPage extends Utils {
    private static String URL = "http://8.137.19.140:9090/blog_login.html";
    public LoginPage() {
        super(URL);
    }

    /**
     * 检查登录界面是否正常
     * @throws IOException
     */
    public void checkPageRight() throws IOException {
        //检查菜单
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)"));
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)"));

        //检查登录框
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#submit"));

        //打印登录界面
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    /**
     * 登录成功-输入正确的账号和密码
     */
    public void loginSuccess() {
        //清空输入框中的内容
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        //输入账号密码
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();

        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("body > div.nav > a:nth-child(6)")));
        //检查是否跳转成功 -- 通过查看是否存在注销
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)"));
    }

    /**
     * 登录异常情情况：
     *              1.输入正确的账号和错误的密码
     */
    public void loginFail1() throws InterruptedException {
        //清空输入框中的内容
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        //输入账号密码
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        Thread.sleep(500);
        driver.findElement(By.cssSelector("#submit")).click();
        //处理警告弹窗 等待弹窗出现后再点击 - 非页面元素
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        alert.accept();
    }
    /**
     * 登录异常情情况：
     *              2.输入错误的账号和正确的密码
     */
    public void loginFail2() {
        //清空输入框中的内容
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        //输入账号密码
        driver.findElement(By.cssSelector("#username")).sendKeys("zs");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();

        //处理警告弹窗 等待弹窗出现后再点击 - 非页面元素
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
//        System.out.println(alert.getText());
        alert.accept();
    }
    /**
     * 登录异常情情况：
     *              3.输入错误的账号和密码
     *              4.账号为空，密码不为空
     *              5.账号不为空，密码为空
     *              6.账号密码均为空
     */
}
